<?php

include_once($_SERVER["DOCUMENT_ROOT"]. "/core/inc.php");
include_once($_SERVER["DOCUMENT_ROOT"]. "/include/user/inc.php");

$g_cgival = array();
$g_show = array();
$g_pro = array();

if(! security_check::check_refer())
{
	throw new Exception("来路不明");
}

function check_cgi_pro()
{
	global $g_cgival, $g_show, $g_pro;
	cgi::both($g_cgival['t'],"t","json");
	$g_cgival['t'] = string::un_html($g_cgival['t']);

	cgi::both($g_cgival['callback'], "callback", "");
	$g_cgival['callback'] = string::un_html(string::un_script_code($g_cgival['callback']));
	
	
	cgi::both($g_cgival['desc'],"desc","");
	$g_cgival['desc'] = string::un_html($g_cgival['desc']);
	
	cgi::both($g_cgival['sex'],"sex",1);
	
	cgi::both($g_cgival['province'],"province",1000);
	
	cgi::both($g_cgival['user_nickname'],"user_nickname","");
	$g_cgival['user_nickname'] = string::un_html($g_cgival['user_nickname']);
	
	cgi::both($g_cgival['user_realname'],"user_realname","");
	$g_cgival['user_realname'] = string::un_html($g_cgival['user_realname']);

	
	cgi::both($g_cgival['oldpassword'],"oldpassword","");
	$g_cgival['oldpassword'] = string::un_html($g_cgival['oldpassword']);
	
	cgi::both($g_cgival['password'],"password","");
	$g_cgival['password'] = string::un_html($g_cgival['password']);
	
	cgi::both($g_cgival['re_password'],"re_password","");
	$g_cgival['re_password'] = string::un_html($g_cgival['re_password']);
	
}


function get_data()
{
	global $g_cgival, $g_show, $g_pro;
	
	$desc = $g_cgival['desc'] ;
	$sex = $g_cgival['sex'] ;
	$province = $g_cgival['province'] ;
	$user_nickname = $g_cgival['user_nickname'] ;
	$user_realname = $g_cgival['user_realname'] ;
	
	$oldpassword = $g_cgival['oldpassword'] ;
	$password = $g_cgival['password'] ;
	$re_password = $g_cgival['re_password'] ;
	
	$user_info = new user_info();
	$is_login = $user_info->is_logined();
	if($is_login)
	{
		$user_detail_info = $user_info->get_cur_user_info();
		
		$uid = $user_detail_info["uid"];
		
	}
	else
	{
		
		throw new Exception("未登录");
	}	
	
	
	
	preg_match("/^([\x{4e00}-\x{9fa5}0-9A-Za-z_\-]+)$/u",$user_nickname,$match);
	
	if(!$match)
	{
		throw new Exception("昵称支持中英文，数字，_或-");
	}
	
	if($password)
	{
		if($password != $re_password)
		{
			throw new Exception("两次密码输出不同！");
		}
		
		$md5_password = $user_info->encryption($oldpassword);
		if($md5_password != $user_detail_info["password"])
		{
			throw new Exception("老密码错误！");
		}
		$new_password = $user_info->encryption($password);
		
	}

	
	
	
	$db_user_info = new db_user_info();
	$res = $db_user_info->update_user_info_by_uid($uid,"","",$user_nickname,$user_realname,$province,$sex,$desc,$new_password);
	if(!$res)
	{
		throw new Exception("数据库操作失败");
	}
}

 
try
{
	check_cgi_pro();
	get_data();

	$g_show["error"] = "0";
	$g_show["errmsg"] = "操作成功";
}
catch (Exception $e)
{
	$g_show["error"] = "1";
	$g_show["errmsg"] = $e->getMessage();
}
show_pro();
exit();


function show_pro()
{
	global $g_cgival, $g_pro, $g_show, $config;
	
	if ($g_cgival['t'] == "serialize")
	{
		echo serialize($g_show);
	}
	elseif ($g_cgival['t'] == "json")
	{
		echo output::json("json", $g_show, $g_cgival['callback']);
	}
	elseif($g_cgival['t'] == "jsonp")
	{
		echo output::json("jsonp", $g_show, $g_cgival['callback']);
	}
	elseif($g_cgival['t'] == "perl")
	{
		$string = php_to_perl::php_array_to_perl_hash($g_show);
		$string = mb_convert_encoding($string, "GBK", "UTF-8");
		echo $string;
	}
	elseif($g_cgival['t'] == "iframe")
	{
		header("location:{$g_cgival['link']}");
	}
	else
	{
		echo output::json("jsonp", $g_show, $g_cgival['callback']);
	}
}